package com.example.campuslink.viewModel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.invitation.InvitationTable
import com.example.campuslink.commen.db.message.MessageTable
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.commen.db.userNumber.UserNumberTable
import com.example.campuslink.commen.enums.MessageTypeEnum
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ShopViewModel : BaseViewModel() {
    private val _secondInvitationList = MutableLiveData<List<InvitationTable>>(mutableListOf())
    val secondInvitationList get() = _secondInvitationList
    var userNumberInfo = UserNumberTable()
    var schoolInfo = SchoolInfoTable()
    private val _tabIndex = MutableLiveData<Int>(0)
    val tabIndex get() = _tabIndex
    private val _typeValue = MutableLiveData<String>()
    val typeValue get() = _typeValue

    fun setType(type: String){
        _typeValue.postValue(type)
    }

    fun setIndex(index: Int) {
        _tabIndex.postValue(index)
    }


    fun unCollection(invitation: InvitationTable) {

        viewModelScope.launch(Dispatchers.IO) {
//            val invitation = invitationDao.getInvitationById(invitation1.id, invitation1.userNumber)
            val userNumber =
                userNumberDao.getUserNumber(Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: "")
            invitation?.collectionThisInvitationNumber?.remove(
                Sp.getString(
                    context,
                    SpKey.USER_NUMBER.key,
                    ""
                ) ?: ""
            )
            userNumber?.collectList?.removeAll { it.id == invitation.id && it.userNumber == invitation.userNumber }
            userNumber?.browseList?.forEach {
                if (it.id == invitation.id && it.userNumber == invitation.userNumber) {
                    it.collectionThisInvitationNumber = invitation.collectionThisInvitationNumber
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            userNumber?.publishList?.forEach {
                if (it.id == invitation.id && it.userNumber == invitation.userNumber) {
                    it.collectionThisInvitationNumber = invitation.collectionThisInvitationNumber
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            invitationDao.updateInvitation(invitation ?: return@launch)
            userNumberDao.updateUserNumber(userNumber ?: return@launch)
            schoolInfo.shoppingInvitationList.forEach {
                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    it.collectionThisInvitationNumber = invitation.collectionThisInvitationNumber
                }
            }
            schoolInfoDao.updateSchoolInfo(schoolInfo)

            getInvitation(_typeValue?.value?:"推荐")
        }
    }

    fun collection(invitation: InvitationTable) {

        viewModelScope.launch(Dispatchers.IO) {
            val userNumber =
                userNumberDao.getUserNumber(Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: "")
            val schoolInfoTable =
                schoolInfoDao.getSchoolInfoBySchoolName(userNumber?.schoolName ?: "")
            if (userNumber?.collectList == null) {
                userNumber?.collectList = mutableListOf()
            }
            if (invitation?.collectionThisInvitationNumber == null) {
                invitation?.collectionThisInvitationNumber = mutableListOf()
            }
            if (schoolInfoTable?.messageList == null) {
                schoolInfoTable?.messageList = mutableListOf()
            }
//            userNumber?.collectList?.remove(invitation)
            userNumber?.collectList?.removeAll { it.id == invitation.id && it.userNumber == invitation.userNumber }
            invitation?.collectionThisInvitationNumber?.remove(
                Sp.getString(
                    context,
                    SpKey.USER_NUMBER.key,
                    ""
                ) ?: ""
            )

            if (invitation?.userNumber != Sp.getString(context, SpKey.USER_NUMBER.key, "")) {
                var messageTable = MessageTable()
//                messageTable.id = messageDao.getMaxId() ?: 0 + 1
                messageTable.messageImage = invitation?.invitationImage?.get(0) ?: ""
                messageTable.invitationId = invitation?.id ?: 0
                messageTable.messageTime = System.currentTimeMillis()
                messageTable.messageType = MessageTypeEnum.BE_LOVE_INVITATION.value
                messageTable.sendNumber = Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""
                messageTable.handNumber = invitation?.userNumber ?: ""
                messageTable.messageContent =
                    "用户${Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: ""}收藏了你的帖子"
                messageDao.insertMessage(messageTable)
                schoolInfoTable?.messageList?.add(messageTable)
                schoolInfoDao.updateSchoolInfo(schoolInfoTable ?: return@launch)
            }



            userNumber?.collectList?.add(invitation)
            invitation?.collectionThisInvitationNumber?.add(
                Sp.getString(
                    context,
                    SpKey.USER_NUMBER.key,
                    ""
                ) ?: ""
            )
            invitationDao.updateInvitation(invitation ?: return@launch)
            userNumberDao.updateUserNumber(userNumber ?: return@launch)
            schoolInfo.shoppingInvitationList.forEach {

                if (it.id == invitation.id && it.userNumber == it.userNumber) {
                    Log.i("TAG", "collection: ${it}")
                    it.collectionThisInvitationNumber = invitation.collectionThisInvitationNumber
                    Log.i("TAG", "collection: ${it}")
                }
            }
            userNumber?.browseList?.forEach {
                if (it.id == invitation.id && it.userNumber == invitation.userNumber) {
//                    userNumber?.browseList?.remove(it)
//                    userNumber?.browseList?.add(invitation)
                    it.collectionThisInvitationNumber = invitation.collectionThisInvitationNumber
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }

            userNumber?.publishList?.forEach {
                if (it.id == invitation.id && it.userNumber == invitation.userNumber) {
//                    it.copy()
//                    it.copy(collectionThisInvitationNumber = invitation.collectionThisInvitationNumber)
                    it.collectionThisInvitationNumber = invitation.collectionThisInvitationNumber
                    it.invitationParentCommend = invitation.invitationParentCommend
                    it.invitationChildrenCommend = invitation.invitationChildrenCommend
                }
            }
            userNumberDao?.updateUserNumber(userNumber ?: return@launch)
            schoolInfoDao.updateSchoolInfo(schoolInfo)

            getInvitation(_typeValue?.value?:"推荐")

        }
    }

    fun addBrowser(invitation: InvitationTable) {
        viewModelScope.launch(Dispatchers.IO) {
            val userNumber =
                userNumberDao.getUserNumber(Sp.getString(context, SpKey.USER_NUMBER.key, "") ?: "")
            if (userNumber?.browseList == null) {
                userNumber?.browseList = mutableListOf()
            }
            userNumber?.browseList?.removeAll { it.id == invitation.id && it.userNumber == invitation.userNumber }
            userNumber?.browseList?.add(invitation)
            userNumberDao.updateUserNumber(userNumber ?: return@launch)
        }
    }


    fun getInvitation(type: String) {
        val temp = mutableListOf<InvitationTable>()
        viewModelScope.launch(Dispatchers.IO) {
            userNumberInfo = userNumberDao.getUserNumber(
                Sp.getString(
                    context ?: return@launch,
                    SpKey.USER_NUMBER.key ?: "",
                    ""
                ) ?: ""
            )!!
            schoolInfo = schoolInfoDao.getSchoolInfoBySchoolName(userNumberInfo.schoolName)
            schoolInfo.shoppingInvitationList.forEach {
                // TODO: 测试
//                Log.i("TAG", "getInvitation: ${it}")
                if (it.invitationType.contains(type)) {
                    temp.add(it)
                }
            }
            _secondInvitationList.postValue(temp)
        }
    }
}